package com.gaattc.dao;

import com.gaattc.model.Admin;
import com.gaattc.model.bo.AdminLoginBO;
import com.gaattc.model.bo.ChangePwdBO;
import com.gaattc.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl implements AdminDao {

    @Override
    public int login(AdminLoginBO loginBO) {
        //JDBC
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Long query = null;
        try {
            query = (Long) runner.query("select count(id) from admin where email = ? and pwd = ?",
                    new ScalarHandler(), loginBO.getEmail(), loginBO.getPwd());
        } catch (SQLException e) {
            return 500;
        }
        return query != 0 ? 200 : 404;
    }

    @Override
    public List<Admin> allAdmins() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query("select * from admin", new BeanListHandler<Admin>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    @Override
    public Admin getAdminsInfo(int id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = null;
        try {
            admin = queryRunner.query("select * from admin where id = ?", new BeanHandler<Admin>(Admin.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public int addAdmins(Admin admin){
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            return queryRunner.update("insert into admin values (?, ?, ?, ?)", admin.getId(), admin.getEmail(), admin.getNickname(), admin.getPwd());
        } catch (SQLException e) {
            return 10000;
        }
    }

    @Override
    public int updateAdminss(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            return queryRunner.update("update admin set email = ?, nickname = ?, pwd = ? where id = ?",
                    admin.getEmail(), admin.getNickname(), admin.getPwd(), admin.getId());
        } catch (SQLException e) {
            return 10000;
        }
    }

    @Override
    public int deleteAdmins(int id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            return queryRunner.update("delete from admin where id = ?", id);
        } catch (SQLException e) {
            return 10000;
        }
    }

    @Override
    public List<Admin> getSearchAdmins(Admin admin) {
        String email = "%" + admin.getEmail() + "%";
        String nickname = "%" + admin.getNickname() + "%";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> list = null;
        try {
            list = queryRunner.query("select * from admin where email like ? and nickname like ?",
                    new BeanListHandler<Admin>(Admin.class), email, nickname);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int changePwd(ChangePwdBO changePwdBO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            return queryRunner.update("update admin set pwd = ? where email = ?",
                    changePwdBO.getNewPwd(), changePwdBO.getAdminToken());
        } catch (SQLException e) {
            e.printStackTrace();
            return 10000;
        }
    }
}
